package com.eliza.module.view.work.sherlockholmes

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.work.ArrayCreatingInputMerger
import androidx.work.OneTimeWorkRequest
import androidx.work.WorkInfo
import androidx.work.WorkManager
import com.eliza.module.view.work.db.TestDatabase
import com.eliza.module.view.work.db.WordCount
import java.util.Arrays

class AnalyzeSherlockHolmesActivity : ComponentActivity() {
    val mStringBuilder = StringBuilder()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            var tt by remember {
                mutableStateOf("")
            }
            Button(onClick = {

            }) {
                Text(text = "AnalyzeSherlockHolmes")
            }


            Text(text = tt)


            TestDatabase.getInstance(this).wordCountDao.wordCounts.observe(
                this
            ) { wordCounts: List<WordCount>? ->
                if (wordCounts == null) {
                    return@observe
                }
                tt = getWordCountsString(wordCounts)
            }

        }


    }

    private fun enqueueWork(loading: Boolean) {
        val workManager = WorkManager.getInstance(this@AnalyzeSherlockHolmesActivity)

        // Cancelling all work just to make it easier to track what is happening here and make it
        // more insulated.
        workManager.cancelAllWork()

        val textReducingWork: OneTimeWorkRequest =
            OneTimeWorkRequest.Builder(TextReducingWorker::class.java)
                .setInputMerger(ArrayCreatingInputMerger::class.java)
                .build()

        val startupWork: OneTimeWorkRequest =
            OneTimeWorkRequest.Builder(TextStartupWorker::class.java).build()

        workManager
            .beginWith(startupWork)
            .then(
                Arrays.asList(
                    TextMappingWorker.create("advs.txt").build(),
                    TextMappingWorker.create("case.txt").build(),
                    TextMappingWorker.create("lstb.txt").build(),
                    TextMappingWorker.create("mems.txt").build(),
                    TextMappingWorker.create("retn.txt").build()
                )
            )
            .then(textReducingWork)
            .enqueue()

        workManager.getWorkInfoByIdLiveData(textReducingWork.id).observe(
            this
        ) { status: WorkInfo? ->
            val loading =
                (status != null && status.state != WorkInfo.State.SUCCEEDED && status.state != WorkInfo.State.FAILED)
//            mProgressBar.setVisibility(if (loading) View.VISIBLE else View.GONE)
//            mResultsView.setVisibility(if (loading) View.GONE else View.VISIBLE)
//            mAnalyzeButton.setEnabled(!loading)
        }
    }

    private fun getWordCountsString(wordCounts: List<WordCount>): String {
        // TODO: not efficient, this should be part of its own LiveData thing.
        mStringBuilder.setLength(0)
        for (wc in wordCounts) {
            mStringBuilder
                .append(wc.mWord)
                .append(" ")
                .append(wc.mCount)
                .append("\n")
        }
        return mStringBuilder.toString()
    }

}